//
//  PublishVC.m
//  MyTest
//
//  Created by zhiyou on 16-4-14.
//  Copyright (c) 2016年 wangyang. All rights reserved.
//

#import "PublishVC.h"
#import "WYtextView.h"
@interface PublishVC () <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) UIView *barView;
@property (nonatomic, strong) UIButton *addBtn;
@property (nonatomic, strong) WYtextView *titleView;
@property (nonatomic, strong) WYtextView *contentView;
@end

@implementation PublishVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发表晒物";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setBar];
    [self layout];
}

- (void)layout
{
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addBtn.frame = CGRectZero;
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"copy_icon_pengyouquan"] forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_addBtn];
    
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(_barView.mas_bottom).offset(30);
        make.width.height.equalTo(self.view.mas_width).multipliedBy(.25);
    }];
    
    _titleView = [[WYtextView alloc] initWithFrame:CGRectZero];
    _titleView.placeholder = @"添加一个标题";
    [self.view addSubview:_titleView];
    
    [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addBtn.mas_right).offset(10);
        make.top.equalTo(_barView.mas_bottom).offset(10);
        make.width.equalTo(self.view.mas_width).multipliedBy(.6);
        make.height.equalTo(_addBtn.mas_height);
    }];
    
//    UIView *line = [[UIView alloc]initWithFrame:CGRectZero];
//    line.backgroundColor = [UIColor redColor];
//    [self.view addSubview:line];
//    
//    [line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(10);
//        make.right.offset(-10);
//        make.top.equalTo(_addBtn.mas_bottom).offset(10);
//        make.width.equalTo(self.view.mas_width).multipliedBy(.3);
//    }];
    
    _contentView = [[WYtextView alloc] initWithFrame:CGRectZero];
    _contentView.placeholder = @"在这里分享您的购买或使用心得吧";
    [self.view addSubview:_contentView];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(_addBtn.mas_bottom).offset(10);
        make.height.width.offset(Width*0.8);
    }];
}
- (void)selectPhoto
{
    UIActionSheet *sheetView = [[UIActionSheet alloc] initWithTitle:@"请选择封面照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil];
    [sheetView showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    if (buttonIndex == 0) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
    if (buttonIndex == 1) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [_addBtn setBackgroundImage:image forState:UIControlStateNormal];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 创建barView
- (void)setBar
{
    _barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 64)];
    [self.view addSubview:_barView];
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:_barView.frame];
    bgView.image = [UIImage imageNamed:@"topbar"];
    [_barView addSubview:bgView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.center = CGPointMake(_barView.center.x, _barView.center.y+10);
    titleLabel.bounds = CGRectMake(0, 0, 200, 44);
    titleLabel.text = @"发表晒物";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [_barView addSubview:titleLabel];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 20, 40, 40);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"abc"] forState:UIControlStateNormal];
    //backBtn.backgroundColor = [UIColor redColor];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [_barView addSubview:backBtn];
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.frame = CGRectMake(Width-50, 20, 50, 40);
    [commitBtn setTitle:@"发表" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitMessage) forControlEvents:UIControlEventTouchUpInside];
    [_barView addSubview:commitBtn];
}
- (void)commitMessage
{
    NSLog(@">>>>>>");
}
- (void)back
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确定离开此界面嘛~~~再想想呗!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
